package com.party.controller.user.myparty;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.SysUser;
import com.party.model.UserInfo;
import com.party.service.info.InfoService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:27
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping(value = "/privateinfo")
public class UserPrivateInfoController {

    @Resource
    SysUserService sysUserService;

    /**
     * 基本新信息
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        SysUser sysUser=new SysUser();
        if ((SysUser) session.getAttribute("sysUser") != null) {
            Integer uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            sysUser=sysUserService.getUser(uid);
        }else{
            modelAndView.setViewName("user/info/privateinfo");
            return modelAndView;
        }

        modelAndView.addObject("sysUser",sysUser);
        String content=new String(sysUser.getContent()+"");
        if("".equals(content)||content==null||"null".equals(content)){
            modelAndView.addObject("content","");
        }else{
            modelAndView.addObject("content",content);
        }

        modelAndView.setViewName("user/info/privateinfo");
        return modelAndView;
    }

    /**
     * 头像
     * @return
     */
    @RequestMapping(value = "portrait")
    public ModelAndView portrait(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        SysUser sysUser=new SysUser();
        if ((SysUser) session.getAttribute("sysUser") != null) {
            Integer uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            sysUser=sysUserService.getUser(uid);
        }else{
            modelAndView.setViewName("user/info/portrait");
            return modelAndView;
        }

        String headurl=sysUser.getHeadurl();
        String terminatedPath="/upload/files/";
        if("".equals(headurl)||headurl==null||"null".equals(headurl)){
            modelAndView.addObject("headurl","");
        }else{
            modelAndView.addObject("headurl",terminatedPath+headurl);
        }

        modelAndView.setViewName("user/info/portrait");
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> addData(
            @RequestParam(value = "uid")Integer uid,
            @RequestParam(value = "passport")String passport,
            @RequestParam(value = "realname")String realname,
            @RequestParam(value = "email")String email,
            @RequestParam(value = "lone")String lone,
            @RequestParam(value = "content")String content,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        String msg="";
        SysUser sysUser=new SysUser();
        sysUser.setUid(uid);
        sysUser.setPassport(passport);
        sysUser.setRealname(realname);
        sysUser.setEmail(email);
        sysUser.setContent(content.getBytes());
        int num=sysUserService.update(sysUser);
        if(num>0){
            msg="修改成功";
        }else{
            msg="修改失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);
        return map;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/upload")
    @ResponseBody
    public Map<String,Object> upload(
            @RequestParam(value = "uid")Integer uid,
            @RequestParam(value = "headurl")String headurl,
            @RequestParam(value = "headname")String headname,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        String msg="";
        SysUser sysUser=new SysUser();
        sysUser.setUid(uid);
        sysUser.setHeadurl(headurl);
        int num=sysUserService.update(sysUser);
        if(num>0){
            msg="修改成功";
        }else{
            msg="修改失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg",msg);
        String terminatedPath="/upload/files/";
        map.put("headurl",terminatedPath+headurl);
        return map;
    }
}

